<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家名称" prop="businessName">
        <el-input
          v-model="queryParams.businessName"
          placeholder="请输入商家名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家粉丝数" prop="businessFan">
        <el-input
          v-model="queryParams.businessFan"
          placeholder="请输入商家粉丝数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司地址" prop="businessAddress">
        <el-input
          v-model="queryParams.businessAddress"
          placeholder="请输入公司地址"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营业执照编号" prop="businessLicenseNum">
        <el-input
          v-model="queryParams.businessLicenseNum"
          placeholder="请输入营业执照编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="公司法定代表人" prop="businessPresent">
        <el-input
          v-model="queryParams.businessPresent"
          placeholder="请输入公司法定代表人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册资本" prop="businessRegisterMoney">
        <el-input
          v-model="queryParams.businessRegisterMoney"
          placeholder="请输入注册资本"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="businessCreatetime">
        <el-date-picker clearable
          v-model="queryParams.businessCreatetime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商家经营范围" prop="businessRange">
        <el-input
          v-model="queryParams.businessRange"
          placeholder="请输入商家经营范围"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商家电话" prop="businessPhone">
        <el-input
          v-model="queryParams.businessPhone"
          placeholder="请输入商家电话"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="保证金" prop="businessPromiseMoney">
        <el-input
          v-model="queryParams.businessPromiseMoney"
          placeholder="请输入保证金"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备用字段" prop="businessOne">
        <el-input
          v-model="queryParams.businessOne"
          placeholder="请输入备用字段"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['lkshop:shop:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['lkshop:shop:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['lkshop:shop:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['lkshop:shop:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商家ID" align="center" prop="businessId" />
      <el-table-column label="商家名称" align="center" prop="businessName" />
      <el-table-column label="商家粉丝数" align="center" prop="businessFan" />
      <el-table-column label="商家性质" align="center" prop="businessType" />
      <el-table-column label="公司地址" align="center" prop="businessAddress" />
      <el-table-column label="营业执照编号" align="center" prop="businessLicenseNum" />
      <el-table-column label="公司法定代表人" align="center" prop="businessPresent" />
      <el-table-column label="注册资本" align="center" prop="businessRegisterMoney" />
      <el-table-column label="创建时间" align="center" prop="businessCreatetime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.businessCreatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="营业执照照片" align="center" prop="businessLicenseImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.businessLicenseImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商家头像" align="center" prop="businessImg" width="100">
        <template #default="scope">
          <image-preview :src="scope.row.businessImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="商家经营范围" align="center" prop="businessRange" />
      <el-table-column label="商家电话" align="center" prop="businessPhone" />
      <el-table-column label="商家状态(0-下线,1-上线)" align="center" prop="businessStatus" />
      <el-table-column label="保证金" align="center" prop="businessPromiseMoney" />
      <el-table-column label="备用字段" align="center" prop="businessOne" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['lkshop:shop:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['lkshop:shop:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商家对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="shopRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商家名称" prop="businessName">
          <el-input v-model="form.businessName" placeholder="请输入商家名称" />
        </el-form-item>
        <el-form-item label="商家粉丝数" prop="businessFan">
          <el-input v-model="form.businessFan" placeholder="请输入商家粉丝数" />
        </el-form-item>
        <el-form-item label="公司地址" prop="businessAddress">
          <el-input v-model="form.businessAddress" placeholder="请输入公司地址" />
        </el-form-item>
        <el-form-item label="营业执照编号" prop="businessLicenseNum">
          <el-input v-model="form.businessLicenseNum" placeholder="请输入营业执照编号" />
        </el-form-item>
        <el-form-item label="公司法定代表人" prop="businessPresent">
          <el-input v-model="form.businessPresent" placeholder="请输入公司法定代表人" />
        </el-form-item>
        <el-form-item label="注册资本" prop="businessRegisterMoney">
          <el-input v-model="form.businessRegisterMoney" placeholder="请输入注册资本" />
        </el-form-item>
        <el-form-item label="创建时间" prop="businessCreatetime">
          <el-date-picker clearable
            v-model="form.businessCreatetime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="营业执照照片" prop="businessLicenseImg">
          <image-upload v-model="form.businessLicenseImg"/>
        </el-form-item>
        <el-form-item label="商家头像" prop="businessImg">
          <image-upload v-model="form.businessImg"/>
        </el-form-item>
        <el-form-item label="商家经营范围" prop="businessRange">
          <el-input v-model="form.businessRange" placeholder="请输入商家经营范围" />
        </el-form-item>
        <el-form-item label="商家电话" prop="businessPhone">
          <el-input v-model="form.businessPhone" placeholder="请输入商家电话" />
        </el-form-item>
        <el-form-item label="保证金" prop="businessPromiseMoney">
          <el-input v-model="form.businessPromiseMoney" placeholder="请输入保证金" />
        </el-form-item>
        <el-form-item label="备用字段" prop="businessOne">
          <el-input v-model="form.businessOne" placeholder="请输入备用字段" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Shop">
import { listShop, getShop, delShop, addShop, updateShop } from "@/api/lkshop/shop"

const { proxy } = getCurrentInstance()

const shopList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    businessName: null,
    businessFan: null,
    businessType: null,
    businessAddress: null,
    businessLicenseNum: null,
    businessPresent: null,
    businessRegisterMoney: null,
    businessCreatetime: null,
    businessLicenseImg: null,
    businessImg: null,
    businessRange: null,
    businessPhone: null,
    businessStatus: null,
    businessPromiseMoney: null,
    businessOne: null
  },
  rules: {
    businessName: [
      { required: true, message: "商家名称不能为空", trigger: "blur" }
    ],
    businessCreatetime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    businessStatus: [
      { required: true, message: "商家状态(0-下线,1-上线)不能为空", trigger: "change" }
    ],
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询商家列表 */
function getList() {
  loading.value = true
  listShop(queryParams.value).then(response => {
    shopList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    businessId: null,
    businessName: null,
    businessFan: null,
    businessType: null,
    businessAddress: null,
    businessLicenseNum: null,
    businessPresent: null,
    businessRegisterMoney: null,
    businessCreatetime: null,
    businessLicenseImg: null,
    businessImg: null,
    businessRange: null,
    businessPhone: null,
    businessStatus: null,
    businessPromiseMoney: null,
    businessOne: null
  }
  proxy.resetForm("shopRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.businessId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加商家"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _businessId = row.businessId || ids.value
  getShop(_businessId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改商家"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shopRef"].validate(valid => {
    if (valid) {
      if (form.value.businessId != null) {
        updateShop(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addShop(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _businessIds = row.businessId || ids.value
  proxy.$modal.confirm('是否确认删除商家编号为"' + _businessIds + '"的数据项？').then(function() {
    return delShop(_businessIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('lkshop/shop/export', {
    ...queryParams.value
  }, `shop_${new Date().getTime()}.xlsx`)
}

getList()
</script>
